//
//  RankViewController.m
//  jinnengExam
//
//  Created by mac on 2020/8/17.
//  Copyright © 2020 jznrj. All rights reserved.
//

#import "RankViewController.h"

@interface RankViewController ()
@property(nonatomic,strong) NSString *url;
@property(nonatomic) int page;
@end

@implementation RankViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"排行榜";
    UINavigationBar * bar = self.navigationController.navigationBar;
    UIImage *bgImage = [ColorUtil navigationBarColor:[ColorUtil titleColor] BarSize:bar.frame alphe:1.0];
    [bar setBackgroundImage:bgImage forBarMetrics:UIBarMetricsDefault];
    [bar setTintColor:[UIColor whiteColor]];
    [[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor clearColor]}forState:UIControlStateNormal];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,nil]];
    
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
    

    // Do any additional setup after loading the view.
    self.page = 1;
    self.url = [NSString stringWithFormat:@"%@/userPoint/finUserInCompanyRankByPage" ,[URLUtil hostName]];
    [self initView];
}
-(void) viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.hidden =NO;
    self.tabBarController.tabBar.hidden =YES;
    self.tabBarController.tabBar.frame = CGRectMake(0, 0, 0, 0);
}
-(void) initView{
   [self.view addSubview:self.jznWebView];
    [self.jznWebView mas_makeConstraints:^(MASConstraintMaker *make) {
         make.left.right.equalTo(self.view);
         make.top.bottom.equalTo(self.view);
     }];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.jznWebView loadLocalFile:@"/ios_jinneng/userRank" type:@"html"];
    
    self.jznWebView.scrollView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self requestPoint];
        
    }];
    
    self.jznWebView.scrollView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
           
           [self requestMorePoint:++self.page];
       }];
    
    [self registerJsFunction];
}

-(void) registerJsFunction{
    [self.webViewBridge registerHandler:@"changeType" handler:^(id data, WVJBResponseCallback responseCallback) {
        int type = [data[@"type"] intValue];
        self.page = 1;
        switch (type) {
            case 11:
                self.url = [NSString stringWithFormat:@"%@/userPoint/finUserInCompanyRankByPage" ,[URLUtil hostName]];
                break;
            case 12:
               self.url = [NSString stringWithFormat:@"%@/userPoint/finUserInCompanyYearRankByPage" ,[URLUtil hostName]];
               break;
            case 13:
               self.url = [NSString stringWithFormat:@"%@/userPoint/finUserInCompanyMonthRankByPage" ,[URLUtil hostName]];
               break;
            case 21:
                self.url = [NSString stringWithFormat:@"%@/userPoint/findJinnengRankByPage" ,[URLUtil hostName]];
                break;
            case 22:
                self.url = [NSString stringWithFormat:@"%@/userPoint/findJinnengYearRankByPage" ,[URLUtil hostName]];
                break;
            case 23:
                self.url = [NSString stringWithFormat:@"%@/userPoint/findJinnengMonthRankByPage" ,[URLUtil hostName]];
                break;
            case 31:
                self.url = [NSString stringWithFormat:@"%@/userPoint/findDepartmentRankByPage" ,[URLUtil hostName]];
                break;
            case 32:
                self.url = [NSString stringWithFormat:@"%@/userPoint/findDepartmentYearRankByPage" ,[URLUtil hostName]];
                break;
            case 33:
                self.url = [NSString stringWithFormat:@"%@/userPoint/findDepartmentMonthRankByPage" ,[URLUtil hostName]];
                break;
            case 41:
                self.url = [NSString stringWithFormat:@"%@/userPoint/findCompanyRankByPage" ,[URLUtil hostName]];
                break;
            case 42:
                self.url = [NSString stringWithFormat:@"%@/userPoint/findCompanyYearRankByPage" ,[URLUtil hostName]];
                break;
            case 43:
                self.url = [NSString stringWithFormat:@"%@/userPoint/findCompanyMonthRankByPage" ,[URLUtil hostName]];
                break;
            default:
                self.url = [NSString stringWithFormat:@"%@/userPoint/finUserInCompanyRankByPage" ,[URLUtil hostName]];
                break;
        }
        [self requestPoint];
    }];
    
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{
    [self requestPoint];
}
-(void) requestPoint{
    NSDictionary *params = @{@"userId":[NSString stringWithFormat:@"%d",self.loginDto.userId],@"pageNum":@"1",@"pageSize":@"10"};
    [JZNRequestUtil requestUrl:self.url requestParams:params success:^(DataResult *dataResult) {
       if(dataResult.code == 0){
           
           NSString *clear = [NSString stringWithFormat:@"getPointRank(%@)",[dataResult.data mj_JSONString]];
           
           [self.jznWebView evaluateJavaScript:clear completionHandler:nil];
           [self.jznWebView.scrollView.mj_header endRefreshing];
           [self.jznWebView.scrollView.mj_footer resetNoMoreData];
       }else {
       }
    } failure:^(NSError *error) {
           
    }];
}
-(void) requestMorePoint:(int) page{
    NSDictionary *params = @{@"userId":[NSString stringWithFormat:@"%d",self.loginDto.userId],@"pageNum":[NSString stringWithFormat:@"%d",page],@"pageSize":@"10"};
    [JZNRequestUtil requestUrl:self.url requestParams:params success:^(DataResult *dataResult) {
       if(dataResult.code == 0){
           
           NSString *clear = [NSString stringWithFormat:@"addPointRank(%@)",[dataResult.data mj_JSONString]];
           [self.jznWebView evaluateJavaScript:clear completionHandler:nil];
           NSArray *array = dataResult.data[@"rankList"];
           if([array count]<10){
               
               [self.jznWebView.scrollView.mj_footer endRefreshingWithNoMoreData];
           }else{
               
               [self.jznWebView.scrollView.mj_footer endRefreshing];
           }
       }else {
       }
    } failure:^(NSError *error) {
           
    }];
}
@end
